<?php

namespace app\common\model\publics;

use think\Model;


class Indexcateclick extends Model
{


	// 设置当前模型使用的完整数据库连接信息
	protected $connection = 'database.db_second';//连接配置
    // 表名
    protected $name = 'publics_indexcate_click';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    /*获取用户点击最多的分类*/
    public static function getUsersClickMany($user_id, $circle_id){
        return self::where(['user_id'=>$user_id, 'circle_id'=>$circle_id])
            ->order("num desc")->order("updatetime desc")->limit(14)->column('cate_id');
    }

    public static function getCateClick($user_id, $circle_id, $cate_id){
        $click = self::where(['user_id'=>$user_id, 'circle_id'=>$circle_id, 'cate_id'=>$cate_id])->find();
        if($click){
            return $click;
        }else{
            return false;
        }
    }

    public static function addCateClick($user_id, $circle_id, $cate_id){
        $click = self::getCateClick($user_id, $circle_id, $cate_id);
        if($click){
            $click->num = $click->num + 1;
            $click->updatetime = time();
            $click->save();
        }else{
            $click = new self();
            $click->circle_id = $circle_id;
            $click->user_id = $user_id;
            $click->cate_id = $cate_id;
            $click->num = 1;
            $click->updatetime = time();
            $click->createtime = time();
            $click->save();
        }
        return $click;
    }

}
